import Vue from 'vue'
import Router from 'vue-router'



Vue.use(Router)

let router  = new Router({
  routes: [
    {
      path: '/',
      name: 'Login',
      component: ()=>import('../components/views/Login.vue'),
      children: [
        {
          path: '/',
          component: ()=>import('../components/login/Login'),
        }
      ]
    },
    {
      path: '/UserView',
      name: 'UserView',
      component: () => import('../components/views/UserView.vue'),
      meta: { isAuth: true, title:'主页' },
      children: [
        {
          path:'/UserView',
          redirect:'/UserView/UserIndex'　　//默认显示
        },
        {
          path:'/UserView/UserIndex',
          redirect:'/UserView/UserIndex/photo'　　//默认显示
        },
        {
          path: '/UserView/UserIndex',
          component:()=>import('../components/index/UserIndex.vue'),
          children:[
            {
              path:'/UserView/UserIndex/photo',
              component:()=>import('../components/index/babyView/BabyPhotos.vue'),
            },
            {
              path:'/UserView/UserIndex/growthRecord',
              component:()=>import('../components/index/babyView/GrowthRecord.vue'),
            },
            {
              path:'/UserView/UserIndex/importantEvents',
              component:()=>import('../components/index/babyView/ImportantEvents.vue'),
            },
            {
              path:'/UserView/UserIndex/relatives',
              component:()=>import('../components/index/babyView/Relatives.vue'),
            },
            {
              path:'/UserView/UserIndex/vaccination',
              component:()=>import('../components/index/babyView/Vaccination.vue'),
            },
          ]
        },
     /*   {
          path:'/UserView/UserIndex/Parenting',
          component:()=>import('../components/index/Parenting.vue')
        },*/
        {
          path:'/UserView/UserIndex/Recommend',
          component:()=>import('../components/index/community/Recommend.vue')
        },
        /*{
          path:'/UserView/UserIndex/QuestionsAndAnswers',
          component:()=>import('../components/index/community/QuestionsAndAnswers.vue')
        },*/
        {
          path:'/UserView/UserIndex/Mine',
          component:()=>import('../components/index/community/Mine.vue')
        },
        {
          path:'/UserView/UserIndex/Follow',
          component:()=>import('../components/index/community/Follow.vue')
        },
        {
          path:'/UserView/UserIndex/Buy',
          component:()=>import('../components/index/shopping_mall/Buy.vue')
        },
        {
          path:'/UserView/UserIndex/Orders',
          component:()=>import('../components/index/shopping_mall/Orders.vue')
        },
        {
          path:'/UserView/UserIndex/PointsCenter',
          component:()=>import('../components/index/shopping_mall/PointsCenter.vue')
        },
        {
          path:'/UserView/UserIndex/Sell',
          component:()=>import('../components/index/shopping_mall/Sell.vue')
        },
        {
          path:'/UserView/UserIndex/ShoppingCart',
          component:()=>import('../components/index/shopping_mall/ShoppingCart.vue')
        },
        {
          path:'/UserView/UserIndex/Message',
          component:()=>import('../components/index/Message.vue')
        }
      ]
    },
    {
      path:'/PersonalCenter',
      component:()=>import('../components/views/PersonalCenter.vue'),
      meta: { isAuth: true, title:'主页' },
    }
  ]
})

//全局前置路由守卫————初始化的时候被调用、每次路由切换之前被调用
router.beforeEach((to, from, next) => {
  if (to.meta.isAuth){
    if (sessionStorage.getItem('user')!==null) {
      next()  //放行
    } else {
      alert('请登录！')
      return next('/')
    }
  }else {
    next()
  }
})


export default router

